﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using BoldBrick.SharePoint.CommonControls.Extensions;

namespace BoldBrick.OneIntranet.HR.DataObjects.Lists.WorkflowHistoryListData
{
	
	public class HRWorkflowHistoryItem
	{
		public int CandidateId { get; set; }

		public SPWorkflowHistoryEventType EventType { get; set; }

		public WorkflowHistoryListDataValue Data { get; set; }

		public Guid WorkflowHistoryParentInstanceID { get; set; }

		public DateTime DateOccured { get; set; }
		
		public HRWorkflowHistoryItem(SPListItem itemWorkflowHistory)
		{
			// candidate ID
			int? candidateId = itemWorkflowHistory.GetValue<int>(SharePoint.CommonControls.DataObjects.SiteColumns.PrimaryItemIdColumn.InternalName);
			if (candidateId.HasValue)
			{
				this.CandidateId = candidateId.Value;
			}

			// event type
			string eventType = itemWorkflowHistory.GetValueObject<string>(SharePoint.CommonControls.DataObjects.SiteColumns.EventTypeColumn.InternalName);
			if (eventType != null)
			{
				int eventTypeInt;
				if (int.TryParse(eventType, out eventTypeInt))
				{
					this.EventType = (SPWorkflowHistoryEventType) eventTypeInt;
				}
			}

			// data
			string data = itemWorkflowHistory.GetValueObject<string>(SharePoint.CommonControls.DataObjects.SiteColumns.DataColumn.InternalName);
			if (data != null)
			{
				Data = new WorkflowHistoryListDataValue(data);
			}

			// WorkflowHistoryParentInstanceID
			string id = itemWorkflowHistory.GetValueObject<string>(SharePoint.CommonControls.DataObjects.SiteColumns.WorkflowHistoryParentInstanceColumn.InternalName);
			if (id != null)
			{
				this.WorkflowHistoryParentInstanceID = new Guid(id);
			}

			// DateOccured
			DateTime? dateTime = itemWorkflowHistory.GetValue<DateTime>(SharePoint.CommonControls.DataObjects.SiteColumns.OccurredColumn.InternalName);
			if (dateTime.HasValue)
			{
				this.DateOccured = dateTime.Value;
			}
		}
	}
}
